<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-col :span="24">
          <el-form-item :label="$t('common.pid')" prop="pid">
            <!--父级选择框-->
            <uni-tree-select v-model="dataForm.pid" ref="parentTree" :selectParams="parentSelectParams"
              :treeParams="parentTreeParams" :styles="styles" :data="parentTreeData" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('sysOssDir.name')" prop="name">
            <!--文本框-->
            <el-input v-model="dataForm.name" :placeholder="$t('sysOssDir.name')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('sysOssDir.value')" prop="value">
            <!--文本框-->
            <el-input v-model="dataForm.value" :placeholder="$t('sysOssDir.value')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('common.sort')" prop="sort">
            <!--计数器-->
            <el-input-number v-model="dataForm.sort" :min="1" :max="50"></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data () {
    return {
      loading: false,
      visible: false,
      styles: {
        width: '100%'
      },
      parentSelectParams: {
        multiple: false,
        clearable: true,
        placeholder: '一级'
      },
      parentTreeParams: {
        filterable: false,
        clickParent: true,
        props: {
          children: 'children',
          label: 'name',
          value: 'id',
          disabled: 'disabled'
        }
      },
      parentTreeData: [],
      dataForm: {
        id: '',
        pid: '',
        name: '',
        value: '',
        pids: '',
        pnames: '',
        sort: '',
        treeLevel: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        arr: [1, 2]
      }
    }
  },
  computed: {
    dataRule () {
      return {
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  components: {},
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/oss/sysOssDir/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/oss/sysOssDir/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
